package com.ourtest.mycouponing.mycouponing;

import adpter.FragmentAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends FragmentActivity {
    /**
     * Called when the activity is first created.
     */
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private ViewPager mPageVp;
    private int currentIndex;//ViewPager的当前选中页
    private RadioButton homepage_btn,privilege_btn,more_btn,setting_btn,nearby_btn;
    private RadioGroup radioGroup;
    private HomepageFragment mHomepageFg;
    private MoreFragment mMoreFg;
    private SettingFragment mSettingFg;
    private PrivilegeFragment mPrivilege;
    private NearbyFragment mNearbyFg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        findById();
        init();
    }

    private void init() {
        mHomepageFg = new HomepageFragment();
        mMoreFg = new MoreFragment();
        mSettingFg = new SettingFragment();
        mPrivilege = new PrivilegeFragment();
        mNearbyFg = new NearbyFragment();
        mFragmentList.add(mHomepageFg);
        mFragmentList.add(mPrivilege);
        mFragmentList.add(mNearbyFg);
        mFragmentList.add(mSettingFg);
        mFragmentList.add(mMoreFg);
        mFragmentAdapter = new FragmentAdapter(
        this.getSupportFragmentManager(), mFragmentList);
        mPageVp.setAdapter(mFragmentAdapter);
        mPageVp.setCurrentItem(0);
        mPageVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /*i :当前页面，及你点击滑动的页面 v:当前页面偏移的百分比*/
            /*i2:当前页面偏移的像素位置 */
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        homepage_btn.setChecked(true);
                        break;
                    case 1:
                        privilege_btn.setChecked(true);
                        break;
                    case 2:
                        nearby_btn.setChecked(true);
                        break;
                    case 3:
                        setting_btn.setChecked(true);
                        break;
                    case 4:
                        more_btn.setChecked(true);
                        break;
                    default:
                        homepage_btn.setChecked(true);
                        break;
                }
            }
            /* state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做*/
            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private void findById() {
        mPageVp = (ViewPager) findViewById(R.id.id_page_vp);
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        homepage_btn = (RadioButton) findViewById(R.id.radio_homepage);
        privilege_btn = (RadioButton) findViewById(R.id.radio_privilege);
        more_btn = (RadioButton) findViewById(R.id.radio_more);
        setting_btn = (RadioButton) findViewById(R.id.radio_setting);
        nearby_btn = (RadioButton) findViewById(R.id.radio_nearby);
    }

}
